package ru.braim.weather_app.services;

import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import ru.braim.weather_app.dto.weather_forecast.WeatherForecastRequestDTO;
import ru.braim.weather_app.dto.weather_forecast.WeatherForecastResponseDTO;
import ru.braim.weather_app.models.Region;
import ru.braim.weather_app.models.WeatherForecast;
import ru.braim.weather_app.repositories.RegionRepository;
import ru.braim.weather_app.repositories.WeatherForecastRepository;
import ru.braim.weather_app.responses.weather_forecast.WeatherForecastResponse;
import ru.braim.weather_app.utils.validators.models.WeatherForecastValidator;

@Service
public class WeatherForecastService {

    private final WeatherForecastRepository weatherForecastRepository;
    private final RegionRepository regionRepository;

    public WeatherForecastService(WeatherForecastRepository weatherForecastRepository, RegionRepository regionRepository) {
        this.weatherForecastRepository = weatherForecastRepository;
        this.regionRepository = regionRepository;
    }

    public WeatherForecastResponse createWeatherForecast(WeatherForecastRequestDTO weatherForecastRequestDTO) {


        Region region = regionRepository.getRegionById(weatherForecastRequestDTO.getRegionId());

        if (region == null) {
            return new WeatherForecastResponse(null, HttpStatus.NOT_FOUND);
        }

        weatherForecastRequestDTO.setRegionId(region.getId());

        WeatherForecast weatherForecast = WeatherForecastValidator.getValidWeatherForecast(weatherForecastRequestDTO);

        if (weatherForecast == null) {
            return new WeatherForecastResponse(null, HttpStatus.BAD_REQUEST);
        }


        weatherForecast.setRegion(region);

        weatherForecastRepository.save(weatherForecast);

        WeatherForecastResponseDTO weatherForecastResponseDTO = new WeatherForecastResponseDTO();

        weatherForecastResponseDTO.setId(weatherForecast.getId());
        weatherForecastResponseDTO.setRegionId(weatherForecast.getRegion().getId());
        weatherForecastResponseDTO.setTemperature(weatherForecast.getTemperature());
        weatherForecastResponseDTO.setWeatherCondition(weatherForecast.getWeatherCondition().getWeatherCondition());
        weatherForecastResponseDTO.setDateTime(weatherForecast.getDateTime());

        return new WeatherForecastResponse(weatherForecastResponseDTO, HttpStatus.OK);
    }

    public WeatherForecastResponse getWeatherForecastById(Long id) {
        if (id <= 0) {
            return new WeatherForecastResponse(null, HttpStatus.BAD_REQUEST);
        }
        
        WeatherForecast weatherForecast = weatherForecastRepository.findById(id).orElse(null);
        
        if (weatherForecast == null) {
            return new WeatherForecastResponse(null, HttpStatus.NOT_FOUND);
        }
        
        WeatherForecastResponseDTO weatherForecastResponseDTO = new WeatherForecastResponseDTO();
        
        weatherForecastResponseDTO.setId(weatherForecast.getId());
        weatherForecastResponseDTO.setDateTime(weatherForecast.getDateTime());
        weatherForecastResponseDTO.setTemperature(weatherForecast.getTemperature());
        weatherForecastResponseDTO.setWeatherCondition(weatherForecast.getWeatherCondition().getWeatherCondition());
        weatherForecastResponseDTO.setRegionId(weatherForecast.getRegion().getId());
        
        return new WeatherForecastResponse(weatherForecastResponseDTO, HttpStatus.OK);
        
    }

    public WeatherForecastResponse updateWeatherForecastById
            (Long id, WeatherForecastRequestDTO weatherForecastRequestDTO) {

        if (id <= 0) {
            return new WeatherForecastResponse(null, HttpStatus.BAD_REQUEST);
        }

        WeatherForecast weatherForecastFromRepository = weatherForecastRepository.findById(id).orElse(null);

        if (weatherForecastFromRepository == null) {
            return new WeatherForecastResponse(null, HttpStatus.NOT_FOUND);
        }

        weatherForecastRequestDTO.setRegionId(weatherForecastFromRepository.getRegion().getId());

        WeatherForecast weatherForecast = WeatherForecastValidator.getValidWeatherForecast(weatherForecastRequestDTO);

        if (weatherForecast == null) {
            return new WeatherForecastResponse(null, HttpStatus.BAD_REQUEST);
        }



        weatherForecastFromRepository.setTemperature(weatherForecast.getTemperature());
        weatherForecastFromRepository.setWeatherCondition(weatherForecast.getWeatherCondition());
        weatherForecastFromRepository.setLocalDateTime(weatherForecast.getDateTime());

        weatherForecastRepository.save(weatherForecastFromRepository);

        WeatherForecastResponseDTO weatherForecastResponseDTO = new WeatherForecastResponseDTO();

        weatherForecastResponseDTO.setId(weatherForecastFromRepository.getId());
        weatherForecastResponseDTO.setDateTime(weatherForecastFromRepository.getDateTime());
        weatherForecastResponseDTO.setTemperature(weatherForecastFromRepository.getTemperature());
        weatherForecastResponseDTO.setWeatherCondition(weatherForecastFromRepository.getWeatherCondition()
                .getWeatherCondition());
        weatherForecastResponseDTO.setRegionId(weatherForecastFromRepository.getRegion().getId());

        return new WeatherForecastResponse(weatherForecastResponseDTO, HttpStatus.OK);

    }

    public WeatherForecastResponse deleteWeatherForecastById(Long id) {
        if (id <= 0) {
            return new WeatherForecastResponse(null, HttpStatus.BAD_REQUEST);
        }

        WeatherForecast weatherForecast = weatherForecastRepository.findById(id).orElse(null);

        if (weatherForecast == null) {
            return new WeatherForecastResponse(null, HttpStatus.NOT_FOUND);
        }

        weatherForecastRepository.delete(weatherForecast);

        return new WeatherForecastResponse(null, HttpStatus.OK);
    }
}
